package com.sinosun.autotestmachine;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import org.json.JSONArray;
import org.xutils.x;

import com.sinosun.autotestmachine.tools.EnvTag;
import com.sinosun.autotestmachine.tools.TestConfig;
import com.sinosun.autotestmachine.tools.reader.ReadMode;
import com.sinosun.mstplib.remote.mstplib.util.DeviceId;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class App extends Application {

	private static App instance;
	private static TestConfig testConfig;
	private String CACHE_FILE_DIR = "/AutoTest/";
	private String LOG_FILE_DIR = "log/";
	private String CONFIG_FILE_DIR = "config/";
	private String CERT_FILE_DIR = "cert/";
	private String UPLOAD_FILE_DIR = "upLoadFile/";
	private String DOWNLOAD_FILE_DIR = "downloadFile/";
	private String TEST_CASE_DIR = "TestCase/";
	private String TEST_CONFIG_DIR = "testConfig/";
	private String logPath;
	private String configPath;
	private String certPath;
	private String uploadPath;
	private String downloadPath;
	private String appFilePath;
	private String testCasePath;
	private String testConfigPath;
	public final static int TEST_RESULT = 0x3301;
	public final static int TEST_STOP_FLAG = 0x3302;
	public final static int TEST_START_FALG = 0x3303;
	public final static int TEST_TIME_FRESH_FLAG = 0x3304;
	public final static int TEST_REPORT_RESULT_FLAG = 0x3305;
	public final static int TEST_REPORT_ALL_RESULT_FLAG = 0x3306;
	public static com.sinosun.autotestmachine.tools.parser.RunInformation runInformation = new com.sinosun.autotestmachine.tools.parser.RunInformation();
	public final static List<Element> allInfo = new CopyOnWriteArrayList<Element>();
	public final static Map<String, JSONArray> allNetWorkRet = new HashMap<String, JSONArray>();
	public final static String PATH = "path";
	public static ReadMode caseReadMode;
	
	@Override public void onCreate() {
		makeFilePath();
		instance = this;
		testConfig = new TestConfig(EnvTag.HEIHE);
		caseReadMode = ReadMode.valueOf(testConfig.getTaskReadMode());
		DeviceId.setContext(this.getApplicationContext());
	    x.Ext.init(this);
	    x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
	}
	
	/**
	 * 通过上下文获取进程名
	 * @param context
	 * @return
	 */
    protected static String getCurProcess(Context context) {
    	int pid = android.os.Process.myPid();
    	ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    	for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
    		if (appProcess.pid == pid) {
    			return appProcess.processName;
    		}
    	}
    	return null;
    }
	
	public static App getInstance() {
		return instance;
	}
	
	public static TestConfig getTestConfig() {
		return testConfig;
	}
	
	public String getAppFilePath(){
		return appFilePath;
	}
	
	public String getDownloadPath() {
		return downloadPath;
	}
	
	public String getUploadPath() {
		return uploadPath;
	}
	
	public String getCertPath() {
		return certPath;
	}
	
	public String getConfigPath() {
		return configPath;
	}
	
	public String getTestCasePath() {
		return testCasePath;
	}
	
	public String getLogPath() {
		return logPath;
	}
	
	public String getTestConfigPath() {
		return this.testConfigPath;
	}
	
	
	//获取默认的存储路径
    private void makeFilePath() {
    	appFilePath = "";
    	
    	if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    		appFilePath = Environment.getExternalStorageDirectory().getPath() + CACHE_FILE_DIR;
        } else {
        	appFilePath = getApplicationContext().getFilesDir().getPath() + CACHE_FILE_DIR;;
        }
    	
        File dir = new File(appFilePath);
        if (!dir.exists()) {	
            dir.mkdirs();
        }
        
        logPath = appFilePath + LOG_FILE_DIR;
        configPath = appFilePath + CONFIG_FILE_DIR;
        certPath = appFilePath + CERT_FILE_DIR;
        uploadPath = appFilePath + UPLOAD_FILE_DIR;
        downloadPath = appFilePath + DOWNLOAD_FILE_DIR;
        testCasePath = appFilePath + TEST_CASE_DIR;
        testConfigPath = appFilePath + TEST_CONFIG_DIR;
        
        File logDirFile = new File(logPath);
        File configDirFile = new File(configPath);
        File certDirFile = new File(certPath);
        File uploadDirFile = new File(uploadPath);
        File downloadDirFile = new File(downloadPath);
        File testCaseDirFile = new File(testCasePath);
        File testConfigDirFile = new File(testConfigPath);
        
        if (!logDirFile.exists()) {
        	logDirFile.mkdirs();
        }
        
        if(!configDirFile.exists()) {
        	configDirFile.mkdirs();
        }
        
        if (!certDirFile.exists()) {
        	certDirFile.mkdirs();
        }
        
        if (!uploadDirFile.exists()) {
        	uploadDirFile.mkdirs();
        }
        
        if (!downloadDirFile.exists()) {
        	downloadDirFile.mkdirs();
        }
        
        if (!testCaseDirFile.exists()) {
        	testCaseDirFile.mkdirs();
        }
        
        if (!testConfigDirFile.exists()) {
        	testConfigDirFile.mkdirs();
        }
    }
}
